//------------------------------------------------------------Common Operations--------------------------------
function CreateNewFolder() {

    var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=CREATENEWFOLDER&pid=' + curruntFolderId + '&Name=' + $('#facebox #newFolderName')[0].value;
    $.ajax({
        url: 'AjaxController.ashx',
        type: 'post',
        data: postData,
        success: function (rhtml) {
            if (rhtml.split('$')[0] == 'ok') {
                var item = $.parseJSON(rhtml.split('$')[1]);
                $('#facebox .content').html($('#facebox #newFolderName')[0].value + ' is added');
                $('#fileAndFolders').append("<li><img src=\"../TemplateImages/FileExtentions/" + item.ExtentionTitle + ".png\" /><a id=\"f_" + item.FileInfoID + "\" href=\"#\" onclick=\"" + item.JSFunction + "\">" + item.Title + "</a> </li>");
            }
            else
                $('#facebox .content').html(rhtml);
        }
    });
}
function downloadUnknownType(id) {
    Download(id.split('_')[1]);
}
function OpenTextFile(id) {
    //TextFileContent
    $('#showTextFile').trigger('click');
    $('#facebox #insideTextContent').html('<img src="../TemplateImages/loading.gif" />');
     var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=OPENTEXT&fid=' +id.split('_')[1];
     $.ajax({
         url: 'AjaxController.ashx',
         type: 'post',
         data: postData,
         success: function (rhtml) {
             if (rhtml != 'error') {
                 var textArea = '<textarea cols="50" rows="30" style="width:100%;" >' + rhtml + '</textarea>';
                 $('#facebox #insideTextContent').html(textArea);
             }
         }
     });


 }

 function OpenJpgFile(id) {
     var nId = id.split('_')[1];
      var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=OPENPICTURE&fid=' +id.split('_')[1];
      $.ajax({
          url: 'AjaxController.ashx',
          type: 'post',
          data: postData,
          success: function (rhtml) {
              if (rhtml != 'error') {
                  files = rhtml;
                  files = JSON.parse(files)
                  ShowPicture(0);
              }
          }
      });
     $("#photoViewer").show();
 }

 function OpenOggFile(id) {
     var nId = id.split('_')[1];
     var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=OPENOGG&fid=' + id.split('_')[1];
     $.ajax({
         url: 'AjaxController.ashx',
         type: 'post',
         data: postData,
         success: function (rhtml) {
             if (rhtml != 'error') {
                 files = rhtml;
                 files = JSON.parse(files);  
                 $('#moviePlayer').show();
             }
         }
     });
 }

 function OpenMp4File(id) {
     var nId = id.split('_')[1];
     var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=OPENOGG&fid=' + id.split('_')[1];
     $.ajax({
         url: 'AjaxController.ashx',
         type: 'post',
         data: postData,
         success: function (rhtml) {
             if (rhtml != 'error') {
                 files = rhtml;
                 files = JSON.parse(files);
                 $("#jquery_jplayer_1").jPlayer({
                     ready: function () {
                         $(this).jPlayer("setMedia", {
                             m4v:".." +files,

                             poster: "../TemplateImages/Nico_Video_logo.png",

                         });
                     },
                     swfPath: "../Scripts/JQuery/",
                     supplied: "m4v"
                 });
                 $('#moviePlayer').show();
             }
         }
     });
 }

 function OpenZip(id)
 {
  var nId = id.split('_')[1];
     var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=UNZIP&fid=' + id.split('_')[1];
     $.ajax({
         url: 'AjaxController.ashx',
         type: 'post',
         data: postData,
         success: function (rhtml) {
        refresh ();
         }
    });
 }

function  ZipFile(id)
{
 var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=ZIP&fid=' + id;
     $.ajax({
         url: 'AjaxController.ashx',
         type: 'post',
         data: postData,
         success: function (rhtml) {
         if(rhtml==true)
         {
            alert("YES");
         }
         refresh();
         }
    });


 
}
function OpenFolder(id) {
    id = id.split('_')[1];
    $('#HiddenAction').val('OPENFOLDER');
    $('#HiddenArgument').val(id);
    $('#fileMangerForm').submit();
}
function OpenFolderTreeView(id) {
    if ($('#' + id).hasClass('file')) {
        SelectedFile = id;
        //FileRightClickItem('Open') 
     }
    else {
        id = id.split('_')[1];
        if (id == '0') {
            GotoRootFolder();
        }
        else {
            $('#HiddenAction').val('OPENFOLDER_TREE');
            $('#HiddenArgument').val(id);
            $('#fileMangerForm').submit();
        }
    }
}
function GotoParentFolder(id) {
    
    $('#HiddenAction').val('GOTOPARENT');
    $('#HiddenArgument').val(id);
    $('#fileMangerForm').submit();
}
function GotoRootFolder() {
    $('#HiddenAction').val('');
    $('#HiddenArgument').val('');
    $('#fileMangerForm').submit();
}
function Back() {
    $('#HiddenAction').val('BACK');
    $('#HiddenArgument').val('');
    $('#fileMangerForm').submit();
}
function Forward() {
    $('#HiddenAction').val('FORWARD');
    $('#HiddenArgument').val('');
    $('#fileMangerForm').submit();
}
$(document).ready(function () {
    if (forwardEnable)
        $('#ForwardBtn').show()
    if (backEnable)
        $('#BackBtn').show()
});
//------------------------------------------------------------------Right Click---------------------------------------------------------------------
var windowwidth;
var windowheight;
var checkmenu;

$(window).ready(function () {

    $('.StickyNoteMenu').hide();

    $('#mask').click(function () {
        $(this).height(0);
        $(this).width(0);
        $('.StickyNoteMenu').hide(500);
        checkmenu = 0;
        return false;
    });

    $('#mask').bind("contextmenu", function () {
        $(this).height(0);
        $(this).width(0);
        $('.FileRightClickMenu').hide(500);
        $('.FolderRightClickMenu').hide(450);
        checkmenu = 0;
        return false;
    });

    $(window).resize(function () {
        if (checkmenu == 1) {
            windowwidth = $(window).width();
            windowheight = $(window).height();
            $('#mask').css({
                'height': windowheight,
                'width': windowwidth
            });
        }

    });
});
var SelectedFile = 0;
function refresh () {
    $('#HiddenAction').val('REFRESH');
    $('#HiddenArgument').val(curruntFolderId);
  
    $('#fileMangerForm').submit();
 }
 function FileRighClick(e) {
     $('#mask').trigger('click');
    SelectedFile = $(this).find('a').attr('id');
   
    windowwidth = $(window).width();
    windowheight = $(window).height();
    checkmenu = 1;
    $('#mask').css({
        'height': windowheight,
        'width': windowwidth
    });
    $('.FileRightClickMenu').show(500);
    $('.FileRightClickMenu').css({
        'top': e.pageY + 'px',
        'left': e.pageX + 'px'
    });

    return false;
}
function FolderRightClick(e) {
    $('#mask').trigger('click');
    SelectedFile = $(this).find('a').attr('id');

    windowwidth = $(window).width();
    windowheight = $(window).height();
    checkmenu = 1;
    $('#mask').css({
        'height': windowheight,
        'width': windowwidth
    });
    $('.FolderRightClickMenu').show(500);
    $('.FolderRightClickMenu').css({
        'top': e.pageY + 'px',
        'left': e.pageX + 'px'
    });

    return false;
}
function FolderSRighClick(e) {
   // SelectedFile = $(this).attr('id').split('_')[1];
    $('#mask').trigger('click');
    windowwidth = $(window).width();
    windowheight = $(window).height();
    checkmenu = 1;
    $('#mask').css({
        'height': windowheight,
        'width': windowwidth
    });
    $('.FolderSRightClickMenu').show(500);
    $('.FolderSRightClickMenu').css({
        'top': e.pageY + 'px',
        'left': e.pageX + 'px'
    });

    return false;
}
function FolderRightClickItem(action) {
switch (action) {
        case 'Open':
            {

                OpenFolder(SelectedFile);
                break;
            }
        case 'Paste':
            {
                var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=PASTE&fid=' + SelectedFile.split('_')[1];
                var sUrl = 'AjaxController.ashx';

                $.ajax({
                    type: "POST",
                    url: sUrl,
                    data: postData,
                    success: function (rHtml) {
                        if (rHtml.split('$')[0] == 'ok') {

                        }
                        else {
                            alert(rHtml);
                        }
                    },
                    error: function (xhr, textStatus, errorThrown)
                    { }

                });
                break;
            }
            }
            $('#mask').trigger('click');
}
function RightClickItem(action,type){
    switch (action) {
       
        case 'Delete':
            {
                var c = confirm('Are you sure you want to delete this folder?');
                if (c) {
                    var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=DELETE&type='+type+'&fid=' + SelectedFile.split('_')[1];
                    var sUrl = 'AjaxController.ashx';

                    $.ajax({
                        type: "POST",
                        url: sUrl,
                        data: postData,
                        success: function (rHtml) {
                            if (rHtml == 'ok') {
                                $('#' + SelectedFile).parent('li').replaceWith('');
                            }
                            else {
                                alert(rHtml);
                            }
                        },
                        error: function (xhr, textStatus, errorThrown)
                        { }

                    });
                }
                break;
            }
        case 'Cut':
            {
                var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=CUT&type=' + type + '&fid=' + SelectedFile.split('_')[1];
                var sUrl = 'AjaxController.ashx';

                $.ajax({
                    type: "POST",
                    url: sUrl,
                    data: postData,
                    success: function (rHtml) {
                        if (rHtml == 'ok') {
                           
                        }
                        else {
                            alert(rHtml);
                        }
                    },
                    error: function (xhr, textStatus, errorThrown)
                    { }

                });
                break;
            }
        case 'Copy':
            {
                var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=COPY&type=' + type + '&fid=' + SelectedFile.split('_')[1];
                var sUrl = 'AjaxController.ashx';

                $.ajax({
                    type: "POST",
                    url: sUrl,
                    data: postData,
                    success: function (rHtml) {
                        if (rHtml == 'ok') {

                        }
                        else {
                            alert(rHtml);
                        }
                    },
                    error: function (xhr, textStatus, errorThrown)
                    { }

                });
                break;
            }
        case 'Properties':
            {

            showProperties(SelectedFile.split('_')[1],type);
             
                break;
            }
            case 'Zip':
            {
               ZipFile(SelectedFile.split('_')[1],type);
               break;
            }
       

    }
    $('#mask').trigger('click');
}
function FileRightClickItem(action) {
    switch (action) {
        case 'Download':
            {
                Download(SelectedFile.split('_')[1]);
                
                break;
            }
        case 'Open':
            {
                //$('#f_' + SelectedFile.split('_')[1]).trigger('click');
                $('#' + SelectedFile).trigger('click');
                break;
            }
                        case 'Zip':
            {
               ZipFile(SelectedFile.split('_')[1],type);
               break;
            }
       
    }
    $('#mask').trigger('click');
}
function FolderSpaceRight(action) {
    switch (action) {
        case 'CreateNewfolder':
            {
                
                $('#createNewFolder').trigger('click');
                break;
            }
        case 'UploadFile':
            {
                $('#uploadFileButton').trigger('click');
                break;
            }
        case 'Paste':
            {
                
                var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=PASTE&fid=' + curruntFolderId;
                var sUrl = 'AjaxController.ashx';

                $.ajax({
                    type: "POST",
                    url: sUrl,
                    data: postData,
                    success: function (rHtml) {
                        if (rHtml.split('$')[0] == 'ok') {
                            var item = $.parseJSON(rHtml.split('$')[1]);
                            
                            $('#fileAndFolders').append("<li><img src=\"../TemplateImages/FileExtentions/" + item.ExtentionTitle + ".png\" /><a id=\"f_" + item.FileInfoID + "\" href=\"#\" onclick=\"" + item.JSFunction + "\">" + item.Title + "</a> </li>");
                        }
                        else {
                            alert(rHtml);
                        }
                        $('#mask').trigger('click');
                    },
                    error: function (xhr, textStatus, errorThrown)
                    { }

                });
                break;
            }
        case 'SortBySize':
            {
                $('#HiddenAction').val('REFRESH');           
                $('#HiddenArgument').val(curruntFolderId);
                $('#HiddenSort').val('size');
                $('#fileMangerForm').submit();
                break;
            }
        case 'SortByName':
            {
                $('#HiddenAction').val('REFRESH');
                $('#HiddenArgument').val(curruntFolderId);
                $('#HiddenSort').val('title');
                $('#fileMangerForm').submit();
                break;
            }
        case 'Properties':
            {
                if (curruntFolderId == 0) {
                    $('#properties').show();
                }
                else {
                    showProperties(curruntFolderId, 'FOLDER');
                }

                break;
            }
                        case 'Zip':
            {
               ZipFile(SelectedFile.split('_')[1],type);
               break;
            }
           
    }
    $('#mask').trigger('click');
}
//-------------------------------------------------Tree View-------------------------------------------------------------
function treeView(json) {
    var treeInfo = $.parseJSON(json);
    return '<ul class="filetree">' + treeRecursive(treeInfo) + '</ul>';
}
function treeRecursive(treeInfo) {
    var retVal = '';
    var onClick = '';
    if (treeInfo.Type == 'file')
        onClick=treeInfo.JSFunction;
    else
        onClick='OpenFolderTreeView(this.id)';
    retVal += '<li class="' + treeInfo.Class + '" ><span id="' + treeInfo.FileInfoId + '" class="' + treeInfo.Type + '" href="#" onclick="' + onClick + '">' + treeInfo.Item + '</span>';
    var child='';
    if (treeInfo.ChildItems != '') {
        for (var i = 0; i < treeInfo.ChildItems.length; i++) {
            child += treeRecursive(treeInfo.ChildItems[i]);
        }
        child='<ul>'+child+'</ul>';
    }
    return retVal + child + '</li>';

}
//---------------------------------------------------Download----------------------------------------------------------------
function Download(fid) {
    var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=DOWNLOAD&fid=' + fid;
    var sUrl = 'AjaxController.ashx';
    $.download(sUrl,postData);

}
//-----------------------------------------------Properties-------------------------------------------
function showProperties(fid,type) {
    var postData = 'ACTIONTYPE=FILEMANAGER&ACTION=PROPERTIES&type=' + type + '&fid=' + fid;
    var sUrl = 'AjaxController.ashx';

    $.ajax({
        type: "POST",
        url: sUrl,
        data: postData,
        success: function (rHtml) {
            if (rHtml.split('$')[0] == 'ok') {
                var fileInfo = $.parseJSON(rHtml.split('$')[1]);
                $('#fileAndFolderProperties #FType').val(fileInfo.Type);
                $('#fileAndFolderProperties #FLocation').val('Root:\\' + fileInfo.Address);
                $('#fileAndFolderProperties #FSize').val(fileInfo.Size);
                $('#fileAndFolderProperties #CDate').val(fileInfo.CreationDate);
                $('#fileAndFolderProperties').show();
            }
            else {
                alert(rHtml);
            }
        },
        error: function (xhr, textStatus, errorThrown)
        { }

    });

}